﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Yichat.MicroService.Service
{
    public class ConfigOptionConfigurationProvider : ConfigurationProvider
    {
        private readonly IConfiguration _configuration;
        public ConfigOptionConfigurationProvider(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async override void Load()
        {
            var host = _configuration["Host"];
            var url = $"{host}/Configs";
            var output = await Common.Utils.InvokeApiAync(url);
            if (output.IsSuccessStatusCode)
            {
                var configs = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(output.Payload);
                Data = new ConcurrentDictionary<string, string>();
                configs.ForEach(c =>
                {
                    Data.Add(c);
                });
            }
        }
    }
}
